﻿/**
  *FileName:    example
  *Date:        23/4/9 周日 下午 7:22:57
  *Author:      Zhou Hang
  *Version:     1.0
  *Description: 
*/
#include "example.h"

// 冰墩墩（bushi）
void bdd()
{
	initgraph(THEIGHT, TWIDTH);
	setorigin(THEIGHT / 2, TWIDTH / 2);
	setbkcolor(WHITE);
	cleardevice();

	pnm_ellipse(0, 0, 110, 140, BLACK);
	pnm_fillcircle(25, -50, 12, BLACK);
	pnm_fillcircle(-25, -50, 12, BLACK);
	pnm_fillcircle(25, -50, 3, WHITE);
	pnm_fillcircle(-25, -50, 3, WHITE);
	pnm_ellipse(0, 40, 15, 10, RED);
	pnm_fillcircle(0, 40, 10, RED);

	int key = _getch();
	closegraph();
}


// 两个相差45度的正方形的裁切以及填充
void FillCutTest()
{
	initgraph(400, 400);
	setbkcolor(WHITE);
	cleardevice();

	int xl = 100, yb = 100, xr = 300, yt = 300;
	Cut cut(xl, yb, xr, yt);
	cut.drawRect(BLACK, RED);

	vector<POINT> p1 = {
		{200, 50}, {50, 200}, {200, 350}, {350, 200}
	};

	Fill f_origin(p1, 4);
	f_origin.fill(BLUE);

	vector<POINT> new_poly = cut.polyCut(p1, 4);

	Fill f(new_poly, new_poly.size());
	f.fill(YELLOW);

	int key = _getch();
	closegraph();
}

// 绘制一只猫（bushi）
void cat()
{
	initgraph(400, 400);
	setbkcolor(WHITE);
	cleardevice();

	vector<POINT> head = {
		{100,200},{150,300},{250,300},{300,200},
		{270,100},{250,50},{220,100},{170,100},
		{150,50},{120,100}
	};
	Curve c;
	c.drawParabola(head, BLACK, true);
	pnm_fillcircle(170, 170, 6, BLACK);
	pnm_fillcircle(230, 170, 6, BLACK);

	setfillcolor(YELLOW);
	floodfill(120, 170, BLACK);

	vector<POINT> nose1 = {
		{200,180},{200,200},{180,210}
	};
	vector<POINT> nose2 = {
		{200,180},{200,200},{220,210}
	};
	c.drawParabola(nose1, BLACK);
	c.drawParabola(nose2, BLACK);

	int key = _getch();
	closegraph();
}

// 求两个点的中点
POINT middle_point(POINT p1, POINT p2) {
	int x = (p1.x + p2.x) / 2;
	int y = (p1.y + p2.y) / 2;
	return { x, y };
}

// 根据四个点绘制橘子瓣
void orange_part(vector<POINT> ps, COLORREF color) {
	Curve c;
	c.draw2Bezier(ps, color);
	mp_line(ps[3], ps[2], color);
	mp_line(ps[3], ps[0], color);
	POINT mp = middle_point(ps[1], ps[3]);
	setfillcolor(color);
	floodfill(mp.x, mp.y, color);
}

// 绘制一个钟表（bushi）
void orangeClock()
{
	initgraph(400, 400);
	setbkcolor(0x87CEEB);
	cleardevice();
	setorigin(200, 200);
	pnm_fillcircle(0, 0, 170, YELLOW);
	pnm_fillcircle(0, 0, 165, WHITE);
	//bresenham_cicle(200, 200, 150, BLACK);

	ZEigen::Rotate3f r15(15);
	ZEigen::Rotate3f r15_n(-15);
	ZEigen::Rotate3f r36(36);

	POINT start = { -150, 0 };
	POINT top_start = ZEigen::TransformPoint(r15, start);
	POINT bottom_start = ZEigen::TransformPoint(r15_n, start);
	vector<POINT> part_start{ bottom_start, {-180, 0}, top_start, {-10, 0} };

	COLORREF colors[] = {
		0xFFB6C1, 0xFFB6C1, 0xE6E6FA, 0x87CEEB, 0x98FB98,
		0xFAFAD2, 0xF4A460, 0xE9967A, 0xCD5C5C, 0xC0C0C0
	};

	for (int i = 0; i < 10; i++) {
		part_start = ZEigen::TransformPoints(r36, part_start);
		orange_part(part_start, colors[i]);
	}
	//saveimage("./img/orange_clock.bmp");
	int key = _getch();
	closegraph();
}

void grass()
{
	initgraph(640, 480);
	setcolor(GREEN);
	setorigin(320, 240);
	double e;
	int x1, x2, y1, y2;
	for (double a = 0; a < 2 * M_PI; a += 2 * M_PI / 720)
	{
		e = 100 * (1 + sin(4 * a));
		x1 = (int)(e * cos(a));
		y1 = (int)(e * sin(a));
		x2 = (int)(e * cos(a + M_PI / 5));
		y2 = (int)(e * sin(a + M_PI / 5));

		mp_line(x1, y1, x2, y2, GREEN);
		Sleep(20);
	}
	int key = _getch();
	closegraph();
}
